/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.odichy.andfml.sax;

import org.odichy.andfml.domain.FMLItem;
import org.odichy.andfml.domain.FMLList;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * AndFML, an FMyLife.com client on Google Android platform.
 *
 * @author Odin Liu<odinushuaia@gmail.com>
 *
 * @version 1.0.0
 */
public class FMLListHandler extends DefaultHandler {
	private FMLList fmlList;
	private FMLItem item;
	private String error;
	private StringBuilder sb;
	
	private int currentState = 0;
	private final int ITEM = 1;
	private final int AUTHOR = 2;
	private final int CATEGORY = 3;
	private final int DATE = 4;
	private final int AGREE = 5;
	private final int DESERVED = 6;
	private final int COMMENTS = 7;
	private final int TEXT = 8;
	private final int PAGES = 9;
	private final int CODE = 10;
	private final int PUB_DATE = 11;
	private final int ERROR = 12;
	@Override
	public void startDocument() throws SAXException {
		fmlList = new FMLList();
	}
	
	@Override
	public void endDocument()throws SAXException {
		
	}
	
	@Override
	public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
		if("root".equals(localName)) {
			currentState = 0;
			return;
		}
		else if("item".equals(localName)) {
			item = new FMLItem();
			item.setId(Integer.parseInt(atts.getValue("id")));
			currentState = ITEM;
			return;
		}
		else if("author".equals(localName))  {
			currentState = AUTHOR;
			item.setAuthorPic(atts.getValue("photo"));
			return;
		}
		else if("category".equals(localName)) {
			currentState = CATEGORY; 
			return;
		}
		else if("date".equals(localName)) {
			currentState = DATE; 
			return;
		}
		else if("agree".equals(localName)) {
			currentState = AGREE; 
			return;
		}
		else if("deserved".equals(localName)) {
			currentState = DESERVED; 
			return;
		}
		else if("comments".equals(localName)) {
			currentState = COMMENTS; 
			return;
		}
		else if("text".equals(localName)) {
			currentState = TEXT;
			sb = new StringBuilder();
			return;
		}
		else if("pages".equals(localName)) {
			currentState = PAGES; 
			return;
		}
		else if("code".equals(localName)) {
			currentState = CODE; 
			return;
		}
		else if("pubdate".equals(localName)) {
			currentState = PUB_DATE; 
			return;
		}
		else if("error".equals(localName)) {
			currentState = ERROR; 
			return;
		}
		currentState = 0;
	}
	
	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		if("item".equals(localName)) {
			fmlList.addFMLItem(item);
			return;
		}
		else if("error".equals(localName)) {
			fmlList.addError(error);
			return;
		}
		else if("text".equals(localName)) {
			item.setText(sb.toString());
			return;
		}
	}
	
	@Override
	public void characters(char[] ch, int start, int length) {
		String content = new String(ch, start, length);
		switch(currentState) {
		case AUTHOR:
			item.setAuthor(content);
			currentState = 0;
			break;
		case CATEGORY:
			item.setCategory(content);
			currentState = 0;
			break;
		case DATE:
			item.setDate(content);
			currentState = 0;
			break;
		case AGREE:
			item.setAgree(Integer.parseInt(content));
			currentState = 0;
			break;
		case DESERVED:
			item.setDeserved(Integer.parseInt(content));
			currentState = 0;
			break;
		case COMMENTS:
			item.setComments(Integer.parseInt(content));
			currentState = 0;
			break;
		case TEXT:
			sb.append(content);
			currentState = TEXT;
			break;
		case PAGES:
			fmlList.setTotalPages(Integer.parseInt(content));
			currentState = 0;
			break;
		case CODE:
			fmlList.setCode(Integer.parseInt(content));
			currentState = 0;
			break;
		case PUB_DATE:
			fmlList.setPubdate(content);
			currentState = 0;
			break;
		case ERROR:
			error = content;
			currentState = 0;
			break;
		default:
			return;
		}
	}

	public FMLList getFmlList() {
		return fmlList;
	}
}
